1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import { error, fail, redirect } from '@sveltejs/kit';
import {
archive as archiveProject,
getById,
} from '$lib/server/services/projects';
import {
add,
loadProjectDashboard,
loadArchived,
} from '$lib/server/services/routines';
import {
archiveRoutineAction,
completeRoutineAction,
editRoutineAction,
} from '$lib/server/services/routine-actions';
import {
numericId,
parseFormString,
parseFrequencyDays,
} from '$lib/server/services/validation';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const id = numericId(params.id);
const project = await getById(id);
if (!project || project.archived) {
throw error(404, 'Projet introuvable');
}
const dashboard = await loadProjectDashboard(id, Temporal.Now.instant());
const archived = await loadArchived(id);
return {
project: { id: project.id, name: project.name },
routines: dashboard.routines,
healthScore: dashboard.healthScore,
healthSeries: dashboard.healthSeries,
archived,
};
};
export const actions: Actions = {
addRoutine: async ({ request, params }) => {
const data = await request.formData();
const name = parseFormString(data.get('name'));
if (!name) {
return fail(400, { error: 'Le nom est requis' });
}
try {
await add(
numericId(params.id),
name,
parseFrequencyDays(data.get('frequencyDays')),
);
} catch {
return fail(400, { error: 'Fréquence invalide' });
}
return { success: true };
},
editRoutine: async ({ request }) => {
const data = await request.formData();
return editRoutineAction(data, numericId(data.get('id')));
},
complete: async ({ request }) => {
const data = await request.formData();
return completeRoutineAction(data, numericId(data.get('id')));
},
archiveRoutine: async ({ request }) => {
const data = await request.formData();
return archiveRoutineAction(numericId(data.get('id')));
},
archiveProject: async ({ params }) => {
await archiveProject(numericId(params.id));
throw redirect(303, '/');
},
};
|